package mail_operations;

import java.io.File;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


import java.util.*;


//import javax.activation.*;

public class ReportMailer {

    public static ArrayList<String> mail(String server, String sender, Object[] cc, String receiver, String subject, String body, File file) {
    	try{
    		InternetAddress[] ccList = new InternetAddress[cc.length];
    		for(int i=0;i<cc.length;i++){
    			ccList[i] = (InternetAddress)cc[i];
    		}
            return send(server, receiver, ccList, sender, subject, body, file);
        } 
    	catch (Exception ex){
        	ex.printStackTrace();
        }
        return null;
    }
    private static ArrayList<String> send(String smtpServer, String to, InternetAddress[] cc, String from
    		   , String subject, String body, File file){
    			ArrayList<String> errorList = new ArrayList<String>();
    		    try
    		    {   
    		    	if(file!=null && !file.getName().equals(""))
    		    	{
    		    	  Properties props = System.getProperties();
   	    		      FileDataSource fds;
   	    		      // -- Attaching to default Session, or we could start a new one --
   	    		      props.put("mail.smtp.host", smtpServer);
   	    		      Session session = Session.getDefaultInstance(props, null);
   	    		      // -- Create a new message --
   	    		      Message msg = new MimeMessage(session);
   	    		      // -- Set the FROM and TO fields --
   	    		      msg.setFrom(new InternetAddress(from));
   	    		      msg.setRecipients(Message.RecipientType.TO,
   	    		        InternetAddress.parse(to, false));
   	    		      
   	    		      //Add cc if desired
   	    		      if(cc!=null){
   	   	    		      msg.setRecipients(Message.RecipientType.CC,
   	      	    		      cc);
   	    		      }
   	    		      // -- Set the subject and body text --
   	    		      msg.setSubject(subject);
   	    		      
   	    		      MimeBodyPart textPart = new MimeBodyPart();
   	    		      MimeBodyPart filePart = new MimeBodyPart();
   	    		      Multipart mp = new MimeMultipart();
   	    		      
   	    		      textPart.setText(body);

   	    		      mp.addBodyPart(textPart);
    		    	  fds = new FileDataSource(file);
	    		      filePart.setDataHandler(new DataHandler(fds));
	    		      filePart.setFileName(fds.getName());
    		    	  mp.addBodyPart(filePart);
   	    		      msg.setContent(mp);
   	    		      // -- Set some other header information --
   	    		      msg.setHeader("Auto-Mail", "CC Auto Mailer");
   	    		      msg.setSentDate(new Date());
   	    		      // -- Send the message --
   	    		      try{
   	   	    		      Transport.send(msg);
   	    		      }catch(SendFailedException e){
   	    		    	  try{
   	    		    		  if(!errorList.contains("Invalid Email: " + e.getInvalidAddresses()[0].toString()))
   	    		    			  errorList.add("Invalid Email: " + e.getInvalidAddresses()[0].toString());
   	    		    	  }catch(Exception ex){
   	    		    		  ex.printStackTrace();
   	    		    	  }
   	    		      }
    		    	}
    		    } catch (Exception ex){
    		    	ex.printStackTrace();
    		    }
    		    return errorList;
    		  }
}
